package com.adong.dao.imlp;

import com.adong.dao.BookDao;
import com.adong.pojo.Book;

import java.sql.Connection;
import java.util.List;

/**
 * @author adong
 */
public class BookDaoImpl extends BaseDao<Book> implements BookDao {
    /**
     * 添加图书
     *
     * @param
     * @return INSERT INTO t_book(`id` , `name` , `author` , `price` , `sales` , `stock` , `img_path`)
     * VALUES(NULL , ' 人月神话' , ' 刚哥' , 88.15 , 20 , 80 , 'static/img/default.jpg');
     */
    @Override
    public int addBook(Book book) {
        String sql = "insert into t_book(name,author,price,sales,stock,img_path) values(?,?,?,?,?,?)";
        return update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath());
    }

    /**
     * 删除图书
     *
     * @param
     * @return
     */
    @Override
    public int deleteBookById(Integer id) {
        String sql = "delete from t_book where id = ? ";
        return update(sql, id);
    }

    /**
     * 更新图书
     *
     * @param
     * @return
     */
    @Override
    public int updateBook(Book book) {
        String sql = "update t_book set name=? ,author=?,price=?,sales=?,stock=?,img_path=? where id = ?";
        return update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath(), book.getId());
    }

    /**
     * 根据id查询图书
     *
     * @param bookId
     * @return
     */
    @Override
    public Book queryBookById(Integer bookId) {
        String sql = "select id,name,author,price,sales,stock,img_path from t_book where id = ?";
        return queryForOne(sql, bookId);
    }

    /**
     * 查询全部图书
     *
     * @return
     */
    @Override
    public List<Book> queryBooks() {
        String sql = "select id, name,author,price,sales,stock,img_path from t_book";
        return queryForList(sql);
    }


    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from t_book";
        Number number = (Number) queryGetOneValue(sql);
        return number.intValue();
    }

    public List<Book> queryForPageItems(int begin, int pageSize) {
        String sql = "select  id, name,author,price,sales,stock,img_path from t_book limit ?,?";
        return queryForList(sql, begin, pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByPrice(int min, int max) {
        String sql = "select count(*) from t_book where price between ? and ?";
        Number number = (Number) queryGetOneValue(sql, min, max);
        return number.intValue();
    }

    @Override
    public List<Book> queryForPageItemsByPrice(int begin, int pageSize, int min, int max) {
        String sql = "select  id, name,author,price,sales,stock,img_path from t_book where price between ? and ? order by price limit ?,?";
        return queryForList(sql, min, max, begin, pageSize);
    }
}
